package com.yihua.obj;

import com.yihua.GameWin;
import com.yihua.utils.GameUtils;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

//蛇头
public class HeadObj extends GameObj {
    //定义变量控制方向
    private String direction = "right";

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    public HeadObj(Image image, int x, int y, GameWin gameWin) {
        super(image, x, y, gameWin);
        //键盘监听事件
        this.gameWin.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                changeDirection(e);
            }
        });
    }
    //控制方向移动
    public void changeDirection(KeyEvent e){
        switch (e.getKeyCode()){
            case KeyEvent.VK_A:
                if (!"right".equals(direction)){
                    direction = "left";
                    image= GameUtils.leftImg;
                }
                break;
            case KeyEvent.VK_W:
                if (!"down".equals(direction)){
                    direction = "up";
                    image= GameUtils.upImg;
                }
                break;
            case KeyEvent.VK_S:
                if (!"up".equals(direction)){
                    direction = "down";
                    image= GameUtils.downImg;
                }
                break;
            case KeyEvent.VK_D:
                if (!"left".equals(direction)){
                    direction = "right";
                    image= GameUtils.rightImg;
                }
                break;
        }
    }
    //蛇的移动
    public void move(){
        //蛇身移动
        java.util.List<BodyObj> bodyObjList= this.gameWin.bodyList;
        for (int i = bodyObjList.size()-1; i >=1; i--) {
            bodyObjList.get(i).x=bodyObjList.get(i-1).x;
            bodyObjList.get(i).y=bodyObjList.get(i-1).y;
            //蛇头与身体的判断
            if (x==bodyObjList.get(i).x&&y==bodyObjList.get(i).y){
                //游戏结束
                this.gameWin.state=3;
            }
        }
        bodyObjList.get(0).x=this.x;
        bodyObjList.get(0).y=this.y;
        //蛇头移动
        switch (direction){
            case "up":
                if (y-height!=0){
                    y-=height;
                }else {
                    y=570;
                }
                break;
            case "down":
                if (y+height!=600){
                    y+=height;
                }else {
                    y=30;
                }
                break;
            case "left":
                if (x-width>=0){
                    x-=width;
                }else {
                    x=570;
                }
                break;
            case "right":
                if (x+width!=600){
                    x+=width;
                }else {
                    x=30;
                }
                break;
                default:
                    break;
        }
    }
    @Override
    public void drawSelf(Graphics g) {
        super.drawSelf(g);
        //获取食物
        FoodObj foodObj =this.gameWin.foodObj;
        List<BodyObj> bodyList = this.gameWin.bodyList;
        int newx =0;
        int newy = 0;
        if (this.x==foodObj.x&&this.y==foodObj.y){
            //食物被吃掉并生成新的食物
            this.gameWin.foodObj = foodObj.getFood();
            //分数+1
            this.gameWin.score=this.gameWin.score+1;
            //获取身体的最后一个部位
            BodyObj bodyObj = bodyList.get(bodyList.size() - 1);
            newx=bodyObj.x;
            newy=bodyObj.y;
        }
        //通关判断
        if (this.gameWin.score>=5*this.gameWin.ka){
            this.gameWin.state=4;
//            this.gameWin.score=0;
            this.gameWin.ka++;
            this.gameWin.winScore=this.gameWin.ka*5;
//            List<BodyObj> list = this.gameWin.bodyList;
//            for (int i = 0; i < list.size()-2; i++) {
//                list.remove(i);
//            }
//            this.gameWin.bodyList=list;
        }
        move();
        if (newx!=0&&newy!=0){
            BodyObj bodyObj = new BodyObj(GameUtils.bodyImg,x-30,y,this.gameWin);
            this.gameWin.bodyList.add(bodyObj);
        }
    }
}
